package com.example.springbootlgt.controller;

import com.example.springbootlgt.entity.ResponseMessage;
import com.example.springbootlgt.entity.User;
import com.example.springbootlgt.entity.dto.UserDto;
import com.example.springbootlgt.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Documented;

@RestController
@RequestMapping("/user")
public class UserController {
    //增加
    @Autowired
    IUserService userService;
    @PostMapping
    public ResponseMessage add(@Validated @RequestBody UserDto user){
              User userNew=userService.add(user);
              return ResponseMessage.success(userNew);
    }
    //查询
    @GetMapping("/{userId}")
    public ResponseMessage get(@PathVariable Integer userId){
        User userNew=userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }

    //修改
    @PutMapping
    public ResponseMessage edit(@Validated@RequestBody UserDto user){
        User userNew=userService.edit(user);
        return  ResponseMessage.success(userNew);
    }
    //删除用户
    @DeleteMapping("/{userId}")
    public ResponseMessage delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();
    }
}
